<?php

namespace app\api\controller;

use app\common\controller\BasicApi;
use app\common\model\UserModel;

/**
 * Class Wechat
 */
class Wechat extends BasicApi
{
    /**
     * 获取小程序配置
     * @return array
     */
    private function config()
    {
        return config('wechat.miniapp');
    }

    /**
     * 设置用户信息
     * 对应微信新接口 wx.getUserProfile
     */
    public function setUserProfile(){
        $phone = $this->request->post('phone');
        $iv = $this->request->post('iv');
        $content = $this->request->post('encryptedData');
        $session =  $this->request->post('session_key');
        $result = \We::WeMiniCrypt($this->config())->decode($iv, $session, $content);
        $openid = $this->request->post('token') ?: $this->header['token'];
        if(!empty($result)){
            $info = [
                'avatar'    => $result['avatarUrl'],
                'nickname' => config('database.charset') == 'utf8mb4' ? $result['nickName'] : emoji_encode($result['nickName']),
                'realname' => config('database.charset') == 'utf8mb4' ? $result['nickName'] : emoji_encode($result['nickName']),
                'gender'    => isset($result['gender']) ? $result['gender'] : '',
                'phone'    => isset($phone) ? $phone: '',
                'create_time'   => date('Y-m-d H:i:s',time())
            ];
            $member = UserModel::where(['openid'=>$openid])->find();
            if(!empty($member)){
                unset($info['realname']);
                $res = UserModel::where(['openid'=>$openid])->update($info);
            }else if (!empty(UserModel::where(['phone'=>$phone])->find())){
                unset($info['realname']);
                $info['openid']=$openid;
                $res = UserModel::where(['phone'=>$phone])->update($info);
            }else{
                $info['openid']=$openid;
                $res = UserModel::create($info);
            }
            $this->success('用户信息更新成功！', $res);
        }
        $this->error('用户信息更新失败', $result);
    }

    /**
     * 设置用户私密信息
     * 对应微信新接口 wx.getUserInfo
     */
    public function setUserInfo(){
        $iv = $this->request->post('iv');
        $content = $this->request->post('encryptedData');
        $code = $this->request->post('code');
        $ret = \We::WeMiniCrypt($this->config())->session($code);
        if(!isset($ret['session_key'])){
            $this->error("微信code解码失败",$ret);
        }
        $result = \We::WeMiniCrypt($this->config())->decode($iv, $ret['session_key'], $content);
        if(!empty($result)){
            $info = [
                'openid'        => $result['openId'],
                'create_time'   => date('Y-m-d H:i:s',time())
            ];
            $userinfo = UserModel::where(['openid' => $info['openid']])->find();
            if(!empty($userinfo)){
                $this->success('自动登录成功！', [
                    'token' => $info['openid'],
                    'member_id' => $userinfo['id'],
                    'session_key' => $ret['session_key']
                ]);
            }else{
                $this->success('自动登录成功！', [
                    'token' => $info['openid'],
                    'session_key' => $ret['session_key']
                ]);
            }
        }
        $this->error("小程序登录失败!");
    }

}